<?php

namespace app\api\model\hotel;

use app\api\model\hotel\Hotel as HotelModel;
use think\Model;
use traits\model\SoftDelete;

class Hotel extends Model
{

    use SoftDelete;

    

    // 表名
    protected $name = 'hotel';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'integer';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = 'deletetime';

    // 追加属性
    protected $append = [
        'label_text'
    ];

    public function getLabelTextAttr($value,$data)
    {

        if (!empty($data['label'])) {
            return (array)json_decode($data['label'], true);
        }
        return [];
    }

    //地图列表
    public function maplists($param)
    {
        $city_id = isset($param['city_id'])?$param['city_id']:0;

        $where = [];

        if ($city_id){
            $where['city_id'] = $city_id;
        }

        $list = HotelModel::where($where)
            ->field('hotel_id,hotel_name,image,label,address,lat,lng')
            ->order('weigh desc')
            ->select();

        return $list;
    }

    //列表
    public function lists($param)
    {
        $keyword = $param['keyword'];
        $lng = $param['lng'];
        $lat = $param['lat'];
        $city_id = isset($param['city_id'])?$param['city_id']:0;
        $where = [];

        if ($keyword){
            $where['hotel_name'] = ['like', '%' . $keyword . '%'];
        }
        if ($city_id){
            $where['city_id'] = $city_id;
        }

        $list = HotelModel::where($where)
            ->field('hotel_id,hotel_name,image,label,address,lat,lng')
            ->order('weigh desc')
            ->paginate(10);
        foreach ($list as $k => $item) {

            if ($item[ 'lng' ] && $item[ 'lat' ]&&$lng&&$lat) {
                $distance = getDistance((float) $item[ 'lng' ], (float) $item[ 'lat' ], (float) $lng, (float) $lat);
                $list[ $k ][ 'distance' ] = round($distance / 1000,1);
            } else {
                $list[ $k ][ 'distance' ] = 0;
            }

            $tickets_list = HotelRoom::where(['hotel_id'=>$item['hotel_id']])->column('price');

            $list[ $k ][ 'start_price' ] = count($tickets_list)>0?min($tickets_list):0;
        }

        return $list;
    }



}
